﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using twComps;

namespace Velsto
{
    public partial class frmControlPointsCalculator : Form
    {

        private double constant = 0.0;
        private bool constant_valid = false;

        public frmControlPointsCalculator()
        {
            InitializeComponent(); 
            cb_oper.SelectedIndex = 0;
            this.Text = "Control points calculator 4 Velsto";
        }

        private void inputXYZfileSelectbutton_Click(object sender, EventArgs e)
        {
            OpenFileDialog of1 = new OpenFileDialog();
            of1.Filter = "All files (*.*)|*.*";
            of1.FilterIndex = 1;
            var mb_ret = of1.ShowDialog();
            switch (mb_ret)
            {
                case DialogResult.Cancel:
                    break;
                default:
                    //se.GriddingInFile = of1.FileName;
                    inputXYZfileTextbox.Text = of1.FileName;
                    XYPointFile xy1 = new XYPointFile();
                    //se.ErrorMessage = null;
                    //xy1.savedFiletoGridding(ref se);
                    //if (se.ErrorMessage != null)
                    /*{
                        MessageBox.Show(se.ErrorMessage);
                        return;
                    }*/
                    break;
            }
        }
        

        private void outputXYZbutton_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf1 = new SaveFileDialog();
            sf1.Filter = "All files (*.*)|*.*";
            sf1.FilterIndex = 1;
            var mb_ret2 = sf1.ShowDialog();
            switch (mb_ret2)
            {
                case DialogResult.Cancel:
                    break;
                default:
                    //se.GriddingOutFile = sf1.FileName;
                    outputXYZfileTextBox.Text = sf1.FileName;
                    break;
            }
        }
        
        

        private void runCalcButton_Click(object sender, EventArgs e)
        {
            if (inputXYZfileTextbox.Text.Equals("") ||
                 !File.Exists(inputXYZfileTextbox.Text) ||
                 outputXYZfileTextBox.Text.Equals("")
                )
            {
                MessageBox.Show("Input for calculation incomplete");
                return;
            }
            if (inputXYZfileTextbox.Text.Equals(outputXYZfileTextBox.Text))
            {
                MessageBox.Show("Output file must not be equal to Input file");
                return;
            }
            double newDouble;
            if (!double.TryParse(constantTextBox.Text,
                out newDouble))
            {
                MessageBox.Show("Constant must be Numeric");
                return;
            }
            else
            {
                if (constantTextBox.Text != "")
                {
                    constantTextBox.Text = constantTextBox.Text.Replace(",", ".");
                    constant = parseDoubleString(constantTextBox.Text);
                    constant_valid = true;
                }
                else
                {
                    MessageBox.Show("Constant must not be blank");
                    return;
                }
            }
            string filein = inputXYZfileTextbox.Text;
            string fileout = outputXYZfileTextBox.Text;
            int oper = cb_oper.SelectedIndex + 1;
            XYPointFile xyp1 = new XYPointFile();
            string ErrorMessage = xyp1.controlfileCalculate(filein, fileout, oper, constant);
            if (ErrorMessage != null)
            {
                MessageBox.Show(ErrorMessage);
                return;
            }
            else
            {
                MessageBox.Show("Calculation completed");
            }
            return;
        }

        private void inputXYZfileTextBox_DoubleClick(object sender, EventArgs e)
        {
            CommandLineProcess cmd = new CommandLineProcess();
            cmd.Command = "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
            cmd.Arguments = "\"" + inputXYZfileTextbox.Text.ToString() + "\"";
            bool bSuccess = cmd.Start();
            if (!bSuccess)
            {
                MessageBox.Show("Could not open file " + inputXYZfileTextbox.Text.ToString() + " \nContact Support");
                return;
            }
        }

        private void outputXYZfileTextBox_DoubleClick(object sender, EventArgs e)
        {
            CommandLineProcess cmd = new CommandLineProcess();
            cmd.Command = "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
            cmd.Arguments = "\"" + outputXYZfileTextBox.Text.ToString() + "\"";
            bool bSuccess = cmd.Start();
            if (!bSuccess)
            {
                MessageBox.Show("Could not open file " + outputXYZfileTextBox.Text.ToString() + " \nContact Support");
                return;
            }
        }

        // Show Info
        private void bu_gen_moreinfo_Click(object sender, EventArgs e)
        {
            if (lb_gen_moreinfo.Visible == false)
            {
                lb_gen_moreinfo.Visible = true;
                bu_gen_moreinfo.Text = "Hide Info";
            }
            else
            {
                lb_gen_moreinfo.Visible = false;
                bu_gen_moreinfo.Text = "Info";
            }
        }

        // Method to parse with both decimal separators
        public double parseDoubleString(string sd)
        {
            var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
            var numberFormat = (System.Globalization.NumberFormatInfo)
            currentCulture.NumberFormat.Clone();
            numberFormat.NumberDecimalSeparator = ".";
            double number;
            bool succeeded = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
            if (succeeded)
            {
                return number;
            }
            else
            {
                numberFormat.NumberDecimalSeparator = ",";
                bool succeeded2 = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
                if (succeeded2)
                {
                    return number;
                }
                else
                {
                    MessageBox.Show("**ERROR** Invalid Number");
                    return -99999.9;
                }
            }
        }
    }
}
